package org.ss.psci.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.ss.psci.entity.PmEnd;
import org.ss.psci.entity.PmError;

/**
 * Data access object (DAO) for domain model class PmError.
 * 
 * @see org.ss.psci.entity.PmError
 * @author MyEclipse Persistence Tools
 */

public class PmErrorDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(PmErrorDAO.class);
	// property constants
	public static final String TITLE = "title";
	public static final String ERROR_TYPE = "errorType";
	public static final String ERROR_DATE = "errorDate";
	public static final String CONTENT = "content";
	public static final String REASON = "reason";
	public static final String ANSWER = "answer";
	public static final String PROJECTID = "projectId";

	public List findByProject(Object name) {
		return findByProperty(PROJECTID, name);
	}
	
	
	public PmError copyEntity(PmError dest, PmError orig) {
		if (dest == null || orig == null) {
			return new PmError();
		}if (orig.getTitle() != null) {
			dest.setTitle(orig.getTitle());
		}
		if (orig.getErrorType() != null) {
			dest.setErrorType(orig.getErrorType());
		}
		if (orig.getErrorDate() != null) {
			dest.setErrorDate(orig.getErrorDate());
		}
		if (orig.getContent() != null) {
			dest.setContent(orig.getContent());
		}
		if (orig.getReason() != null) {
			dest.setReason(orig.getReason());
		}
		if (orig.getAnswer()!= null) {
			dest.setAnswer(orig.getAnswer());
		}
	
	
		return dest;
	}
	protected void initDao() {
		// do nothing
	}

	public void save(PmError transientInstance) {
		log.debug("saving PmError instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(PmError persistentInstance) {
		log.debug("deleting PmError instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public PmError findById(java.lang.Integer id) {
		log.debug("getting PmError instance with id: " + id);
		try {
			PmError instance = (PmError) getHibernateTemplate().get(
					"org.ss.psci.entity.PmError", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(PmError instance) {
		log.debug("finding PmError instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding PmError instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from PmError as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByTitle(Object title) {
		return findByProperty(TITLE, title);
	}

	public List findByErrorType(Object errorType) {
		return findByProperty(ERROR_TYPE, errorType);
	}

	public List findByErrorDate(Object errorDate) {
		return findByProperty(ERROR_DATE, errorDate);
	}

	public List findByContent(Object content) {
		return findByProperty(CONTENT, content);
	}

	public List findByReason(Object reason) {
		return findByProperty(REASON, reason);
	}

	public List findByAnswer(Object answer) {
		return findByProperty(ANSWER, answer);
	}

	public List findAll() {
		log.debug("finding all PmError instances");
		try {
			String queryString = "from PmError";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public PmError merge(PmError detachedInstance) {
		log.debug("merging PmError instance");
		try {
			PmError result = (PmError) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(PmError instance) {
		log.debug("attaching dirty PmError instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(PmError instance) {
		log.debug("attaching clean PmError instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static PmErrorDAO getFromApplicationContext(ApplicationContext ctx) {
		return (PmErrorDAO) ctx.getBean("PmErrorDAO");
	}
}